package order.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feign.cart.CartFeignService;
import com.rabbitmq.client.Channel;
import order.domain.OrderItem;
import order.domain.OrderReturnReason;
import order.domain.OrderSetting;
import order.service.OrderItemService;
import order.mapper.OrderItemMapper;
import order.vo.trade.DetailArray;
import order.vo.trade.TradeVo;
import org.apache.commons.lang.RandomStringUtils;
import com.feign.vo.CartInfo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author panghu
 * @description 针对表【oms_order_item(订单项信息)】的数据库操作Service实现
 * @createDate 2023-03-04 21:01:40
 */
@Service
@RabbitListener(queues = {"hello.java.queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem>
        implements OrderItemService {

    @Autowired
    private CartFeignService cartFeignService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public TradeVo findOrderTrade(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!ObjectUtils.isEmpty(token)) {
            // 调用购物车远程服务,获取所有被选中的商品
            List<CartInfo> cartInfoList = cartFeignService.getCheckedCart(token);
            // todo 使用rabbitmq发送接收数据
            //rabbitTemplate.convertSendAndReceive()
            TradeVo tradeVo = new TradeVo();
            tradeVo.setActivityReduceAmount(BigDecimal.ZERO);
            // detailArrayList
            List<DetailArray> detailArrayList = new ArrayList<>();
            BigDecimal orderTotalPrice = BigDecimal.ZERO;
            assert cartInfoList != null;
            for (CartInfo cartInfo : cartInfoList) {
                DetailArray detailArray = new DetailArray();
                detailArray.setImgUrl(cartInfo.getImgUrl());
                detailArray.setOrderPrice(cartInfo.getCartPrice());
                detailArray.setSkuId(cartInfo.getSkuId());
                detailArray.setSkuName(cartInfo.getSkuName());
                detailArray.setSkuNum(cartInfo.getSkuNum());
                detailArray.setSourceId(cartInfo.getSourceId());
                detailArrayList.add(detailArray);
                orderTotalPrice = orderTotalPrice.add(cartInfo.getCartPrice());
            }
            tradeVo.setDetailArrayList(detailArrayList);
            // orderDetailVoList
            // originalTotalAmount
            tradeVo.setOriginalTotalAmount(orderTotalPrice);
            // totalAmount
            tradeVo.setTotalAmount(orderTotalPrice);
            // totalNum
            tradeVo.setTotalNum(cartInfoList.size());
            // tradeNo，生成订单编号
            tradeVo.setTradeNo(RandomStringUtils.randomAlphanumeric(30).toUpperCase());
            // userAddressList
            return tradeVo;
        }
        return null;
    }

    /**
     * RabbitHandler 可以重载区分不同的消息
     *
     * @param message 原生的消息，包括消息头+消息体
     * @param content 具体的实体类，发送时如果数据类型有多种选择，可以根据参数来选择具体队列中的数据
     * @param channel 消息发送得通道
     *                tips：
     *                1、只有当一个消息处理完成了才处理下一个消息
     *                2、同一个服务启动多个：同一个消息只能由一个客户端接收，他们处于竞争关系
     */
    @RabbitHandler
    public void getMessage(Message message, OrderReturnReason content, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 消息确认
        channel.basicAck(deliveryTag, false);
        channel.basicNack(deliveryTag, false, true);
        System.out.println(content);
    }

    @RabbitHandler
    public void getMessage2(Message message, OrderSetting content, Channel channel) {
        System.out.println("getMessage2" + content);
    }


}




